﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crawler_v1.spider.entity;
using Crawler_v1.entity;
using Crawler_v1.spider.exception;
using Crawler_v1.utils;
using System.Threading;

namespace Crawler_v1.spider
{
    class SqlitePipeLine
    {

        private ThreadSpider spider;

        //现在采集了多少条
        private int total = 0;

        private static readonly object lockHelper = new object();

        //要采集多少条，-1代表无穷
        private int crawlerSize = -1;

        public int CrawlerSize
        {
            get { return crawlerSize; }
            set { crawlerSize = value; }
        }

        public void onAttachSpider(ThreadSpider spider)
        {
            this.spider = spider;
        }

        public void save(Page page)
        {
            Article article = page.Data as Article;

            if (!isTitleExists(article.title))
            {

                //检查是否采集总数大于指定的数量
                lock (lockHelper)
                {
                    if (crawlerSize > 0 && total >= crawlerSize)
                    {
                        if (spider.isStarted())//可能有多线程在运行，下面执行只停止一次
                        {
                            spider.stop();
                            spider.SpiderListener.onMsg("数据库中插入了" + total + "条，不再插入, 线程中止");
                        }
                    }
                    else
                    {
                        if (insertArticle(page))
                        {
                            total += 1;
                        }
                    }
                }
                
            }
            else
            {
                spider.SpiderListener.onMsg("文章【" + article.title + "】已经存在数据库，不插入");
            }
        }

        /// <summary>
        /// 插入文章
        /// </summary>
        /// <param name="page"></param>
        public bool insertArticle(Page page)
        {
            Article article = page.Data as Article;

            //
            string sql = @"INSERT into ARTICLE(title,content,time,`column`,parent,url,ispost,`create_time`) 
                            VALUES('{0}','{1}','{2}','{3}','{4}','{5}',0,datetime('now', 'localtime'))";

            //转义单引号
            string content = article.content.Replace("'", "''");

            sql = string.Format(sql,
                article.title, content, article.date.ToString()
                , article.column, article.parent, page.Url
                );


            //int i = SQLiteHelper.ExecuteSql(sql);
            int i = MySqliteHelper.ExecuteSql(sql);

            if (i <= 0)
            {
                throw (new SpiderException("[" + article.title + "]数据库插入失败"));
            }

            return true;
        }

        /// <summary>
        /// 判断文章是否存在
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool isTitleExists(string title)
        {
            string sql = "SELECT count(*) FROM ARTICLE WHERE title='{0}'";
            sql = string.Format(sql, title);

            object dd = MySqliteHelper.GetSingle(sql);
            //object dd = SQLiteHelper.GetSingle(sql);

            if (Convert.ToInt32(dd) == 0)
            {
                return false;
            }
            return true;
        }

    }
}
